package hedspi.k55.sm.model;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Portfolio extends DBmanager {
	// zend update: add attributes and constructor and get method
	private int id;
	private String name;
	private double current_money;
	private double invested_money;
	private int number_of_stock;
	private String notes;
	private List<Stock> list_stock;

	public Portfolio(Context context) {
		super(context);
	}

	public Portfolio(Context context, String name) {
		super(context);
		this.name = "" + name;
		// get info from DB
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_PORTFOLIO, new String[] { _ID, _NAME,
				_CURRENT_MONEY, _INVESTED_MONEY, _NUMBER_STOCK, _NOTES }, _NAME
				+ "=?", new String[] { this.name }, null, null, null);
		if (cursor != null) {
			this.id = Integer.parseInt(cursor.getString(0));
			this.current_money = Double.parseDouble(cursor.getString(2));
			this.invested_money = Double.parseDouble(cursor.getString(3));
			this.number_of_stock = Integer.parseInt(cursor.getString(4));
			this.notes = "" + cursor.getString(5);
			init();
		} else {
			this.id = 0;
			this.current_money = 0.0;
			this.invested_money = 0.0;
			this.number_of_stock = 0;
			this.notes = "";
			init();
		}
	}

	public boolean init() {
		list_stock = new ArrayList<Stock>();
		SQLiteDatabase db = this.getReadableDatabase();
		String query = "SELECT symbol FROM stock ";
		Cursor cursor = db.rawQuery(query, null);
		if (cursor != null) {
			if (cursor.moveToFirst()) {
				do {
					Stock stock = new Stock(context, cursor.getString(0));
					System.out.println("symbol" + stock.getSymbol());
					stock.init();
					this.list_stock.add(stock);
				} while (cursor.moveToNext());
				cursor.close();
				db.close();
				return true;
			}

		}
		return false;
	}

	public List<Stock> getAll() {
		return this.list_stock;
	}

	public boolean update() {
		if (isExist("portfolio", "name", this.name)) {
			// update
			SQLiteDatabase db = this.getWritableDatabase();
			ContentValues value = new ContentValues();

			value.put("name", this.name);
			value.put("currentmoney", this.current_money);
			value.put("investedmoney", this.invested_money);
			value.put("numberstock", this.number_of_stock);
			value.put("note", this.notes);

			int affect = (int) db.update("portfolio", value, "name = ?",
					new String[] { this.name });
			if (affect != 1) {
				return false;
			}
			return true;
		} else {
			// add
			SQLiteDatabase db = this.getWritableDatabase();
			ContentValues value = new ContentValues();

			value.put("name", this.name);
			value.put("currentmoney", this.current_money);
			value.put("investedmoney", this.invested_money);
			value.put("numberstock", this.number_of_stock);
			value.put("note", this.notes);

			int affect = (int) db.insert("portfolio", null, value);
			if (affect == -1) {
				return false;
			}
			db.close();
			return true;
		}
	}

	/*
	 * public Portfolio(int id, String name, Double current, Double invested,
	 * Integer number, String notes) { this.id = id; this.name = new
	 * String(name); this.number_of_stock = number.intValue();
	 * this.current_money = current.doubleValue(); this.invested_money =
	 * invested.doubleValue(); this.notes = new String(notes); }
	 */
	public String getName() {
		return this.name;
	}

	public Double getCurrentMoney() {
		return this.current_money;
	}

	public Double getInvestedMoney() {
		return this.invested_money;
	}

	public Integer getNumberStock() {
		return this.number_of_stock;
	}

	public String getNotes() {
		return this.notes;
	}

	/**
	 * get all stock
	 * 
	 * @return
	 */

	/*
	 * // add all portfolio's stock to list_stock public boolean init() { // is
	 * portfolio exist in Database ? if (this.id > 0) { // exist DBmanager myDB
	 * = new DBmanager(null); SQLiteDatabase db = myDB.getReadableDatabase();
	 * 
	 * String query = "SELECT * FROM" + DBmanager.TABLE_STOCK + "WHERE" +
	 * DBmanager._PORTFOLIO_ID + "=" + this.id; Cursor cursor =
	 * db.rawQuery(query, null); if (cursor != null) { cursor.moveToFirst(); do
	 * { Stock stock = new Stock(cursor.getString(1), cursor.getString(2),
	 * Double.parseDouble(cursor .getString(5)), Double.parseDouble(cursor
	 * .getString(6)), Integer.parseInt(cursor .getString(7)),
	 * Double.parseDouble(cursor .getString(8)), Double.parseDouble(cursor
	 * .getString(9)), Double.parseDouble(cursor .getString(10)),
	 * Double.parseDouble(cursor .getString(11)), Double.parseDouble(cursor
	 * .getString(12))); this.list_stock.add(stock); } while
	 * (cursor.moveToNext()); db.close(); return true; } db.close(); } return
	 * false; }
	 * 
	 * // create public boolean create() { // is exist DBmanager myDB = new
	 * DBmanager(null); SQLiteDatabase db = myDB.getReadableDatabase(); if
	 * (myDB.isExist(DBmanager.TABLE_PORTFOLIO, this.name)) { // this.update();
	 * ? return false; } ContentValues value = new ContentValues();
	 * 
	 * value.put(DBmanager._NAME, this.name);
	 * value.put(DBmanager._CURRENT_MONEY, this.current_money);
	 * value.put(DBmanager._INVESTED_MONEY, this.invested_money);
	 * value.put(DBmanager._NUMBER_STOCK, this.number_of_stock);
	 * value.put(DBmanager._NOTES, this.notes);
	 * 
	 * int insert_id; insert_id = (int) db.insert(DBmanager.TABLE_PORTFOLIO,
	 * null, value); if (insert_id == -1) { return false; } db.close(); return
	 * true;
	 * 
	 * }
	 */

	/**
	 * get stock from portfolio
	 * 
	 * @param stockCode
	 * @return Stock
	 */
	public Stock get(String stockCode) {
		return null;
	}

	/**
	 * Add stock to this portfolio
	 * 
	 * @param stock
	 * @return true if successfully, false if not.
	 * 
	 */
	public boolean add(Stock stock) {
		this.list_stock.add(stock);
		return true;
	}

	/**
	 * Empty or not
	 * 
	 * @return true if empty
	 */
	public boolean isEmpty() {
		return this.list_stock.isEmpty();
	}

	/**
	 * Delete a stock from portfolio
	 * 
	 * @param stock
	 * @return true if successfully
	 */
	public boolean remove(String stockSymbol) {
		for (int i = 0; i < this.list_stock.size(); i++) {
			Stock temp = this.list_stock.get(0);
			if (temp.getSymbol().equalsIgnoreCase(stockSymbol)) {
				this.list_stock.remove(temp);
				return true;
			}
		}
		return false;
	}

	/**
	 * Replaces the stock in the specified location with the specified stock.
	 * 
	 * @param location
	 * @param portfolio
	 * @return true if successfully.
	 */
	public boolean set(int location, Stock stock) {
		return false;
	}

	/**
	 * Reload portfolio content from database
	 * 
	 * @return true if successfully.
	 */
	/*
	 * public boolean reload() { DBmanager myDB = new DBmanager(null);
	 * SQLiteDatabase db = myDB.getReadableDatabase(); Cursor cursor =
	 * db.query(DBmanager.TABLE_PORTFOLIO, new String[] { DBmanager._ID,
	 * DBmanager._NAME, DBmanager._CURRENT_MONEY, DBmanager._INVESTED_MONEY,
	 * DBmanager._NUMBER_STOCK, DBmanager._NOTES }, DBmanager._ID + "=?", new
	 * String[] { "" + this.id }, null, null, null); if (cursor != null) {
	 * this.name = cursor.getString(1); this.current_money =
	 * Double.parseDouble(cursor.getString(2)); this.investied_money =
	 * Double.parseDouble(cursor.getString(3)); this.number_of_stock =
	 * Integer.parseInt(cursor.getString(4)); this.notes = "" +
	 * cursor.getString(5); return true; } else { return false; } }
	 * 
	 * public boolean delete() { DBmanager myDB = new DBmanager(null);
	 * SQLiteDatabase db = myDB.getWritableDatabase(); int delete_var;
	 * delete_var = db.delete(DBmanager.TABLE_PORTFOLIO, DBmanager._ID + "=?",
	 * new String[] { "" + this.id }); db.close(); if (delete_var == 1) { return
	 * true; } return false; }
	 */
	public Double getMktValue(){
		Double total = 0.0;
		for(int i=0;i<this.list_stock.size();i++){
			total += this.list_stock.get(i).getMktValue();
		}
		return total;
	}
	public Double getCostBasic(){
		Double total = 0.0;
		for(int i=0;i<this.list_stock.size();i++){
			total += this.list_stock.get(i).getCostBasic();
		}
		return total;
	}
	public Double getGain(){
		return this.getMktValue() - this.getCostBasic();
	}
	public Double getGainPerc(){
		return this.getGain() / this.getCostBasic();
	}
}
